<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\common\model\AdminOperate;
use think\facade\View;
use think\facade\Request;
use think\facade\Session;
use think\captcha\facade\Captcha;

use app\admin\validate\Admin as validateAdmin;
use app\common\model\Admin as AdminModel;
use app\common\model\AdminRole as AdminRoleModel;
use app\common\logic\AdminLogic;


class Admin extends Base
{
    //列表页
    public function list()
    {
        if(Request::isAjax()){

            $model = new AdminModel();
            $params = input('param.');

            $res = $model->tableData($params);
            return dyajaxReturn(1,'获取成功',$res['data'],'',$res['count']);

        }else {
            return View::fetch();
        }
    }

    //添加
    public function add(){
        if(Request::isPost()){
            $param = input('param.');

            $validate = new validateAdmin;
            $result = $validate->scene($param['act'])->batch(false)->check($param);
            if(!$result){
                return dyajaxReturn(0,$validate->getError());
            }
            $AdminModel = new AdminModel();
            $res = $AdminModel->handle($param);
            return dyajaxReturn($res['code'],$res['msg'],[],url('admin/list'));
        }else {

            $AdminRoleModel = new AdminRoleModel();
            $ad_role_res = $AdminRoleModel -> where('is_open','=',1)
                ->where('role_id','<>',1)
                ->order('role_id desc')->select();

            View::assign('ad_role_res',$ad_role_res);
            View::assign('act','add');
            return View::fetch();
        }
    }

    //修改
    public function edit(){
        $AdminModel = new AdminModel();
        if(Request::isPost()){
            $param = input('param.');
            $validate = new validateAdmin;
            $result = $validate->scene($param['act'])->batch(false)->check($param);
            if(!$result){
                return dyajaxReturn(0,$validate->getError());
            }

            $AdminModel = new AdminModel();
            $res = $AdminModel->handle($param);
            return dyajaxReturn($res['code'],$res['msg'],[],url('admin/list'));
        }else {
            $admin_id = input('param.admin_id');
            $info = $AdminModel -> where('admin_id','=',$admin_id)->find();

            $AdminRoleModel = new AdminRoleModel();
            $ad_role_res = $AdminRoleModel -> where('is_open','=',1)
                ->where('role_id','<>',1)
                ->order('role_id desc')->select();
            View::assign('ad_role_res',$ad_role_res);

            View::assign('act','edit');
            View::assign('admin_id',$admin_id);
            View::assign('info',$info);

            return View::fetch();
        }
    }


    //删除
    public function delete(){
        $id = input('param.id');
        $AdminModel = new AdminModel();
        $res = $AdminModel -> where('admin_id','=',$id)->delete();
        if(!$res){
            return dyajaxReturn(0,'删除失败');
        }
        adminLog("后台管理员删除",$id);
        return dyajaxReturn(1,'删除成功');
    }

    //多删除
    public function deleteMore(){
        $ids = input('param.ids');
        $AdminModel = new AdminModel();
        $res = $AdminModel -> where('admin_id','in',$ids)->delete();
        if(!$res){
            return dyajaxReturn(0,'删除失败');
        }
        adminLog("后台管理员多条删除",$ids);
        return dyajaxReturn(1,'删除成功');

    }


    //登录
    public function login(){
        if (IS_POST) {
            $code = input('post.vertify');
            $username = input('post.username/s');
            $password = input('post.password/s');
            $type = input("param.type");

            $adminLogic = new AdminLogic;
            $return = $adminLogic->login($username, $password,$type);

            if($type == 1){
                $this->redirect((String)SITE_URL.'/admin');
            }else{
                if( !captcha_check($code)) {
                    return dyajaxReturn(0,'验证码错误');
                }
                return dyajaxReturn($return['errcode'],$return['message'],[],SITE_URL.'/admin');
            }
        }else {
//            dump(config('app.dispatch_success_tmpl'));
            return View::fetch();
        }
    }


    //退出登录
    public function logout(){
        $adminLogic = new AdminLogic;
        $adminLogic->logout(session('admin_id'));

        $this->success("退出成功",(string)url('admin/login'));
    }

    //获取验证码
    public function vertify(){
        return Captcha::create('verify');
    }

    public function updatePwd(){
        if (IS_POST) {
            $param = input('param.');
            $password = $param['password'];
            $repassword = $param['repassword'];

            $admin_id = session('admin_id');
            if(strlen($password) < 6){
                return dyajaxReturn(0,'请输入六位以上的密码');
            }
            if($password != $repassword){
                return dyajaxReturn(0,'两次密码不一致');
            }

            $AdminModel = new AdminModel();

            $rand = md5('tangyinzaihangzhou');
            $ec_salt = substr($rand,0,6);
            $data['ec_salt'] = $ec_salt;
            $data['password'] = $AdminModel->encrypt($password,$ec_salt) ;
            $data['update_time'] = time();

            $res = $AdminModel -> where('admin_id','=',$admin_id)->update($data);
            if(!$res){
                return dyajaxReturn(0,'密码修改失败');
            }
            adminLog("后台管理员修改密码",$admin_id);
            return dyajaxReturn(1,'密码修改成功');
        }else{
            return View::fetch();
        }
    }


    //用户操作
    public function adminOperateList(){
        if(IS_AJAX) {
            $param = input("param.");
            $AdminOperate = new AdminOperate();
            $res = $AdminOperate->tableData($param);
            return dyajaxReturn(1, '获取成功', $res['data'], '', $res['count']);
        }else{
            return View::fetch();
        }
    }

}
